/**
 * My simple header template from child directory
 * SimpleIOCalculator.java - ${projectName} - ${organizationName} - ${inceptionYear}
 */
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 04.03.14
 * Time: 22:45
 * To change this template use File | Settings | File Templates.
 */
public class SimpleIOCalculator implements FileSizeCalculator {

    @Override
    public long calculateSize(String fileName) throws IOException {
        File file = new File(fileName);
        return calculateSize(file);
    }

    public long calculateSize(File root) {
        if (root.isFile()) {
            return root.length();
        }
        File[] filesInDirectory = root.listFiles();
        long size = 0;
        if (filesInDirectory != null && filesInDirectory.length != 0) {
            for (File file: filesInDirectory) {
                size += calculateSize(file);
            }
            return size;
        }
        return 0;
    }

    @Override
    public String getName() {
        return "SimpleIOCalculator";
    }
}
